<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\logic;

use app\model\PollingType;

class PollingTypeLogic extends BaseLogic
{

    /**
     * 设置模型
     * @return string
     */
    protected function getModelName(): string
    {
        return PollingType::class;
    }

    /**
     * @Description: 获取详情
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $params
     * @Date: 2022-11-03 10:17:54
     * @version: 1.0.0
     * @return [type]
     */    
    public function getPollingType($params){

        $where['id']  = $params['id'];

        $pidWhere['pid'] = $params['id'];

        $type = PollingType::getPollingType($where);

        $content = PollingType::getPollingType($pidWhere);
        $data = [];
        
        foreach ($type as $key => $value) {
             $data[$key]['id'] = $value['id'];
             $data[$key]['name'] = $value['name'];
             $data[$key]['label'] = '检查类型名称';
        }

        foreach ($content as $key => $value) {
            $data[$key+1]['id'] = $value['id'];
            $data[$key+1]['name'] = $value['content'];
            $data[$key+1]['label'] = '检查内容';
        }

        return $data;
    }
    /**
     * @Description: 修改巡检类型
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $params
     * @Date: 2022-11-03 11:04:17
     * @version: 1.0.0
     * @return [type]
     */    
    public function savePollingType($params){

        $pollingType = new PollingType;
        // halt($params['data'][0]['name']);

        $contentWhere['pid'] = $params['data'][0]['id'];

        // 获取该巡检类型的全部巡检内容
        $contentList = PollingType::getPollingType($contentWhere,'id');


        $firstData = [
            'name'=> $params['data'][0]['name']
        ];

        $typeWhere['id'] = $params['data'][0]['id'];
        // 修改巡检类型
        PollingType::savePollingType($typeWhere, $firstData);

        $id_list = [];
        foreach ($params['data'] as $key => $value) {
           if($key != 0){
                if (isset($value['id'])) {
                    // $id_list[$key] = $value['id'];
                    // array_push($id_list,$value['id']);
                    $id_list[$key] = $value['id'];
                //    if(!$result){
                        $where['id'] = $value['id'];
                        $data = [
                            'name' => $params['data'][0]['name'],
                            'content' => $value['name']
                        ];
                        
                        // 修改没被删除的巡检内容
                       PollingType::savePollingType($where,$data);
                //    }
                } else {
                    $addData = [
                        'name'    => $params['data'][0]['name'],
                        'content' => $value['name'],
                        'pid'     => $params['data'][0]['id']
                    ];
                    // 添加新的巡检内容
                    $pollingType->addContent($addData);
                }
           }
            
        }
        // print_r($id_list);
        // die;
         // 检测巡检内容是否被删除
        $result = $this->deletePollingType($id_list, $contentList);
        return true;
    }

    /**
     * @Description: 删除巡检内容
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $id
     * @param [type] $contentList
     * @Date: 2022-11-03 11:11:45
     * @version: 1.0.0
     * @return [type]
     */
    protected function deletePollingType($id_list,$contentList){

        $result = true;

        $list = [];
       foreach ($contentList as $key => $value) {
            $list[$key] = $value;
       }
        $res = array_diff($list, $id_list);
        foreach ($res as $key => $value) {
            $where['id'] = $value;
           PollingType::deletePollingType($where);
        }
        // // 如果不存在则进行删除
        // if($result){
        //     $where['id'] = $id;
        //     PollingType::deletePollingType($where);
        // }
        return $res;
        
    }
}
